package cn.schoolwow.flow.monitor.module.business.testcase.flow.select;

import cn.schoolwow.ams.domain.block.list.dao.entityTable.EntityTableListBlockOption;
import cn.schoolwow.ams.module.block.listBlock.common.domain.remoteSelect.BlockRemoteSelect;
import cn.schoolwow.ams.module.block.listBlock.common.domain.remoteSelect.RemoteSelectOption;
import cn.schoolwow.flow.monitor.domain.FlowMonitorOption;
import cn.schoolwow.flow.monitor.entity.TestCase;
import cn.schoolwow.flow.monitor.module.common.flow.GetRemoteSelectOptionListCommonFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.springframework.context.ApplicationContext;

import java.util.List;

public class AddTestCaseCategoryRemoteSelectFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        ApplicationContext applicationContext = flowContext.checkInstanceData(ApplicationContext.class);
        FlowMonitorOption flowMonitorOption = applicationContext.getBean(FlowMonitorOption.class);
        EntityTableListBlockOption entityTableListBlockOption = flowContext.checkInstanceData(EntityTableListBlockOption.class);

        List<TestCase> testCaseList = flowMonitorOption.dao.query(TestCase.class)
                .addColumn("categoryName")
                .execute()
                .getList(TestCase.class);

        flowContext.startFlow(new GetRemoteSelectOptionListCommonFlow())
                .putCurrentCompositeFlowData("list", testCaseList)
                .putCurrentCompositeFlowData("fieldName", "categoryName")
                .execute();
        List<RemoteSelectOption> remoteSelectOptionList = flowContext.getData("remoteSelectOptionList", List.class);
        BlockRemoteSelect blockRemoteSelect = new BlockRemoteSelect("categoryName", "用例分类", remoteSelectOptionList);
        blockRemoteSelect.cascade = true;
        entityTableListBlockOption.remoteSelectList.add(blockRemoteSelect);
    }

    @Override
    public String name() {
        return "添加分类下拉框选项";
    }
}
